#include<sys/list.h>
#include<stdlib.h>
#include<assert.h>
#include"gui_view.h"

LIST_HEAD(gui_view_list_head);

int gui_view_init()
{
    list_init(&gui_view_list_head);
    return 0;
}

gui_view_t* gui_view_create()
{
    gui_view_t* view = (gui_view_t*)malloc(sizeof(gui_view_t));
    if (!view)
        return NULL;
    view->view = -1;
    list_init(&view->list);
    list_init(&view->spirit_list_head);
    view->spirit = NULL;
    view->nowait = 0;
    return view;
}

int gui_view_destroy(gui_view_t* view)
{
    if (!view)
        return -1;
    view->view = -1;
    if (list_find(&view->list, &gui_view_list_head))
    {
        list_del(&view->list);
    }
    free(view);
    return 0;
}

void gui_view_exit()
{
    gui_view_t *pview,*nview;
    list_traversal_all_owner_to_next_safe(pview,nview,&gui_view_list_head,list)
    {
        gui_view_remove(pview);
        gui_view_destroy(pview);
    }
}

void gui_view_add(gui_view_t* view)
{
    assert(view);
    list_add_tail(&view->list, &gui_view_list_head);
}

void gui_view_remove(gui_view_t* view)
{
    if (!view)
        return;
    list_del(&view->list);
}

gui_view_t* gui_view_find(int view)
{
    gui_view_t* pview;

    gui_view_traversal(pview)
    {
        if(pview->view==view)
        {
            return pview;
        }
    }
    return NULL;
}